package com.yolov8.service;


import org.springframework.stereotype.Service;
import com.yolov8.task.DetectionTask;
import java.util.*;

@Service
public class DetectionService {
    private final Map<String, DetectionTask> runningTasks = new HashMap<>();

    public synchronized String startTask(String taskId, String streamUrl, String modelPath, String pushUrl, List<String> classNames) {
        if (runningTasks.containsKey(taskId)) {
            return "任务已存在：" + taskId;
        }

        try {
            DetectionTask task = new DetectionTask(streamUrl, modelPath, pushUrl, classNames);
            task.start();
            runningTasks.put(taskId, task);
            return "启动成功：" + taskId;
        } catch (Exception e) {
            e.printStackTrace();
            return "启动失败：" + e.getMessage();
        }
    }

    public synchronized String stopTask(String taskId) {
        DetectionTask task = runningTasks.remove(taskId);
        if (task != null) {
            task.stop();
            return "已停止任务：" + taskId;
        } else {
            return "任务不存在：" + taskId;
        }
    }

    public synchronized Set<String> listTasks() {
        return runningTasks.keySet();
    }
}

